#include "StdAfx.h"
#include "SubtractiveFactory.h"
#include <string>


CSubtractiveFactory::CSubtractiveFactory(void)
{
	m_type = L"saw";
	m_pitch = 1.0;
}


CSubtractiveFactory::~CSubtractiveFactory(void)
{
}

CSubtractiveInstrument *CSubtractiveFactory::CreateInstrument(std::vector<double> saw, std::vector<double> square, std::vector<double> triangle)
{
    CSubtractiveInstrument *instrument = new CSubtractiveInstrument();

	instrument->SetPitch(m_pitch);
	
	if(m_type == L"square")
		instrument->SetWavetable(square);
	else if(m_type == L"triangle")
		instrument->SetWavetable(triangle);
	else
		instrument->SetWavetable(saw);

    return instrument;
}


void CSubtractiveFactory::SetNote(CNote *note)
{
    // Get a list of all attribute nodes and the
    // length of that list
    CComPtr<IXMLDOMNamedNodeMap> attributes;
    note->Node()->get_attributes(&attributes);
    long len;
    attributes->get_length(&len);

    // Loop over the list of attributes
    for(int i=0;  i<len;  i++)
    {
        // Get attribute i
        CComPtr<IXMLDOMNode> attrib;
        attributes->get_item(i, &attrib);

        // Get the name of the attribute
        CComBSTR name;
        attrib->get_nodeName(&name);

        CComVariant value;
        attrib->get_nodeValue(&value);

		if(name =="type")
		{
			m_type = value.bstrVal;
		}
		if(name =="pitch")
		{
			value.ChangeType(VT_R8);
			m_pitch = value.dblVal;
		}
    }

}